package com.hmkj.admin.controller.marketing;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.admin.utils.AdminSessionHelper;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.common.utils.ValidateUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.po.marketing.UserEarningsRanking;
import com.hmkj.core.service.marketing.UserEarningsRankingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class UserEarningsRankingJsonController extends GenericController<Integer, UserEarningsRanking> {
    @Resource
    private UserEarningsRankingService userEarningsRankingService;

    /**
     * 用户盈利排行榜
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping("userRanking/json/list")
    public DtGrid userListJson(String dtGridPager) throws Exception {
        return userEarningsRankingService.getDtGridList(dtGridPager, UserEarningsRanking.class);
    }

    /**
     * 新增用户盈利排行榜
     *
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("userRanking/json/add")
    @RequiresPermissions("sys:userRanking:save")
    public ResultEntity addJson(UserEarningsRanking userEarningsRanking) throws Exception {
        checkModel(userEarningsRanking);
        if (userEarningsRankingService.selectCount(new UserEarningsRanking(t -> {
            t.setUsername(userEarningsRanking.getUsername());
        })) > 0) {
            throw new BussinessException("用户名已存在");
        }
        userEarningsRanking.setCreator(AdminSessionHelper.getCurrAdmin().getName());
        userEarningsRankingService.insert(userEarningsRanking);
        return ok();
    }


    /**
     * 编辑用户盈利排行榜
     *
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("userRanking/json/edit")
    @RequiresPermissions("sys:userRanking:edit")
    public ResultEntity editJson(UserEarningsRanking userEarningsRanking) throws Exception {
        checkModel(userEarningsRanking);
        UserEarningsRanking userEarningsRanking1 = checkPrimaryKey(userEarningsRanking.getId());
        if (!userEarningsRanking1.getUsername().equals(userEarningsRanking.getUsername())) {
            if (userEarningsRankingService.selectCount(new UserEarningsRanking(t -> {
                t.setUsername(userEarningsRanking.getUsername());
            })) > 0) {
                throw new BussinessException("用户名已存在");
            }
        }
        userEarningsRanking.setUpdator(AdminSessionHelper.getCurrAdmin().getName());
        userEarningsRankingService.updateByPrimaryKeySelective(userEarningsRanking);
        return ok();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "userRanking/json/del")
    @RequiresPermissions("sys:userRanking:del")
    public ResultEntity del(Integer id) throws Exception {
        UserEarningsRanking userEarningsRanking = checkPrimaryKey(id);
        userEarningsRankingService.updateByPrimaryKeySelective(new UserEarningsRanking(a -> {
            a.setId(userEarningsRanking.getId());
            a.setDelFlag(UserEarningsRanking.DELFLAG.DELETED.code);
        }));
        return ok();
    }

    private void checkModel(UserEarningsRanking userEarningsRanking) {
        if (StringUtils.isBlank(userEarningsRanking.getUsername())) {
            throw new BussinessException("用户名不能为空不能为空");
        } else if (userEarningsRanking.getUsername().length() > 30) {
            throw new BussinessException("用户名长度不能超过30位");
        } else if (StringUtils.isBlank(userEarningsRanking.getUserCount())) {
            throw new BussinessException("邀请人数不能为空");
        } else if (userEarningsRanking.getUserCount().toString().length() > 15) {
            throw new BussinessException("邀请人长度不能超过15位");
        } else if (StringUtils.isBlank(userEarningsRanking.getProfit())) {
            throw new BussinessException("盈利金额不能为空");
        } else if (userEarningsRanking.getProfit().toString().length() > 20) {
            throw new BussinessException("盈利金额不能超过30位");
        } else if (!ValidateUtils.isPrice(userEarningsRanking.getProfit().toString())) {
            throw new BussinessException("盈利金额格式不正确");
        }
    }

    private UserEarningsRanking checkPrimaryKey(Integer id) throws Exception {
        if (null == id) {
            throw new BussinessException("参数有误");
        } else {
            UserEarningsRanking userEarningsRanking = userEarningsRankingService.selectOne(new UserEarningsRanking(t -> {
                t.setId(id);
                t.setDelFlag(UserEarningsRanking.DELFLAG.NORMAL.code);
            }));
            if (null != userEarningsRanking) {
                return userEarningsRanking;
            } else {
                throw new BussinessException("参数有误");
            }
        }
    }
}
